package me.zk.practice1.s;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhangkai
 * @date 2023/7/7 15:50
 */
public class LS141 {

    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        ListNode pre = head;
        while (pre != null) {
            if (!set.contains(pre)) {
                set.add(pre);
                pre = pre.next;
            } else {
                return true;
            }

        }
        return false;
    }

    public boolean hasCycle2(ListNode head) {
        if (head == null) return false;
        ListNode slow = head;
        ListNode quick = head;

      while (quick.next != null && quick.next.next!=null) {
          slow = slow.next;
          quick = quick.next.next;
          if (slow == quick) {
              return true;
          }
      }
      return false;
    }
}
